//
//  GoodsinfoTableView.m
//  一键联IOS
//
//  Created by 徐绍明 on 15/12/28.
//  Copyright © 2015年 xufei. All rights reserved.
//

#import "GoodsinfoTableView.h"
#import "AddGoodsToGouWuCarCell.h"
#import "ShoppingOfCartVC.h"
@interface GoodsinfoTableView ()

@end

@implementation GoodsinfoTableView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;

    _numberOfGoodsForCar = 1;
    [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
    [HttpsManager requestToGetShangPinFenLeiShuXingProductID:self.productId Completion:^(NSArray *ary) {
        NSLog(@"pro %@",self.productId );
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if (ary) {
            _yanseArray = ary;
            
            
            int yanHeight = (_yanseArray.count == 0)?0:(20+(22*((int)_yanseArray.count/4+1)));
            
            ShangPinFeNLeiShuXing *yanse = _yanseArray[_currentYanSeIndex];
            _chicunArray = yanse.resultAry;
            
            int chiH = (_chicunArray == 0)?0:(20+22*((int)_chicunArray.count/4+1));
            _heigt = 110+88+ yanHeight+chiH ;
            
            [_tableView reloadData];
      
        }else{
            SHOW_ALERT(@"该商品已售罄！");
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,HEIGHT - _heigt, WIDE,_heigt) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.scrollEnabled = NO;
        _tableView.tableFooterView= [[UIView alloc] init];
//        UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDE, 30)];
//        head.backgroundColor = [UIColor lightGrayColor];
//        _tableView.tableHeaderView = head;
        
        [self.view addSubview:_tableView];
        

    }];
    if (_yanseArray) {
        
    }
  
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        return 90;
    }
    else if (indexPath.row==1)
    {
        if (_yanseArray.count==0) {
            return 0;
        }
        
        return 30+(22*(_yanseArray.count/4+1));
    }
    else if (indexPath.row==2)
    {
        
        if (_chicunArray.count == 0) {
            return 0;
        }
        return 30+(25*(_chicunArray.count/4+1));
        
        
        
    }else if(indexPath.row ==3)
    {
        return 44;
    }else if(indexPath.row == 4){
        return 44;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddGoodsToGouWuCarCell * cell = nil;
    if (indexPath.row == 0) {
        
        cell = [[AddGoodsToGouWuCarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"add1"];
        //      cell = [tableView dequeueReusableCellWithIdentifier:@"add1"];
        cell = [[NSBundle mainBundle] loadNibNamed:@"AddGoodsToGouWuCarCell" owner:nil options:nil][0];
        cell.goodImageUrl.image = [UIImage imageNamed:@"头像1"];
        
       
      cell.goodPrice.text = self.price ;
       
            cell.infoNum.text = self.num;
               NSLog(@"num %@,p%@",self.num,self.price);
        cell.infoDesc.text = self.desc;
        
        
        [cell.backBtn addTarget:self action:@selector(exitBtnClick) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if(indexPath.row == 3){
        //cell = [tableView dequeueReusableCellWithIdentifier:@"add2"];
        cell = [[AddGoodsToGouWuCarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"add2"];
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"AddGoodsToGouWuCarCell" owner:nil options:nil][1];
        
        cell.goodNumberLB.text = [NSString stringWithFormat:@"%d",_numberOfGoodsForCar];
        cell.goodAddBtn.tag = 2;
        cell.goodJianBtn.tag = 1;
        [cell.goodJianBtn addTarget:self action:@selector(jianAndAddBtn:) forControlEvents:UIControlEventTouchUpInside];
        [cell.goodAddBtn addTarget:self action:@selector(jianAndAddBtn:) forControlEvents:UIControlEventTouchUpInside];
        
    }else if(indexPath.row ==4){
        cell = [[AddGoodsToGouWuCarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"add4"];
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"AddGoodsToGouWuCarCell" owner:nil options:nil][3];
        cell.queDingBtn.backgroundColor = RGB_COLOR(0x00c49f);
        [cell.queDingBtn addTarget:self action:@selector(queDingBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }else{
        cell = [[AddGoodsToGouWuCarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"add3"];
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"AddGoodsToGouWuCarCell" owner:nil options:nil][2];
        cell.titleLb.text = @[@"颜色分类",@"尺寸"] [indexPath.row -1];
        
        if (indexPath.row == 1){
            if (_yanseArray.count >0) {
                for (int i = 0; i< _yanseArray.count; i++) {
                    ShangPinFeNLeiShuXing *yanse = _yanseArray[i];
                    
                    
                    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
                    bt.frame = CGRectMake((WIDE-5*5)/4 *((_yanseArray.count%4)-1)+5, 20 +(22*_yanseArray.count/4), (WIDE-5*5)/4, 20);
                    
                    [bt setTitle:yanse.attrValueName forState:UIControlStateNormal];
                    [bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
                    [bt setBackgroundImage:[UIImage imageNamed:@"wxq2"] forState:UIControlStateNormal];
                    [bt setBackgroundImage:[UIImage imageNamed:@"wxq1"] forState:UIControlStateSelected];
                    bt.tag = i+100;
                    _yansebtn = bt;
                    if (i == 0) {
                        _yansebtn.selected =YES;
                        _yanseID = yanse.attrValueId;
                    }
                    [bt addTarget:self action:@selector(yanseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                    [cell.contentView addSubview:bt];
                }
            }}
        if (indexPath.row ==2){
            
            if (_chicunArray.count >0) {
                for (int i = 0; i< _chicunArray.count; i++) {
                    ChildrenAttr *chicun = _chicunArray[i];
                    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
                    bt.frame = CGRectMake((WIDE-5*5)/4 *((_chicunArray.count%4)-1)+5, 20+(22*_yanseArray.count/4), (WIDE-5*5)/4, 20);
                    
                    [bt setTitle:chicun.attrValueName forState:UIControlStateNormal];
                    [bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
                    
                    [bt setBackgroundImage:[UIImage imageNamed:@"wxq2"] forState:UIControlStateNormal];
                    [bt setBackgroundImage:[UIImage imageNamed:@"wxq1"] forState:UIControlStateSelected];
                    bt.tag = i+1000;
                    _chicunbtn = bt;
                    if (i == 0) {
                        _chicunbtn.selected =YES;
                        _chicunID = chicun.attrValueId;
                    }
                    [bt addTarget:self action:@selector(chimaBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                    [cell.contentView addSubview:bt];
                }
                
            }
            
        }
        
        
        
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}
-(void)exitBtnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void)yanseBtnClick:(UIButton *)btn{
    
    
    _yansebtn.selected = NO;
    btn.selected = YES;
    _yansebtn = btn;
    _currentYanSeIndex  = (int)btn.tag -100;
    ShangPinFeNLeiShuXing *yanse = _yanseArray[_currentYanSeIndex];
    _yanse = yanse.attrValueName;
    _yanseID = yanse.attrValueId;
    [_tableView reloadData];
}
-(void)chimaBtnClick:(UIButton *)btn{
    _chicunbtn.selected = NO;
    btn.selected = YES;
    _chicunbtn = btn;
    ChildrenAttr *chicun = _chicunArray[btn.tag -1000];
    _chicunID = chicun.attrValueId;
    _chima = chicun.attrValueName;
    NSLog(@"chiID %d",_chicunID);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath  animated:NO];
}


-(void)jianAndAddBtn:(UIButton *)btn{
    if (btn.tag ==1) {
        if (_numberOfGoodsForCar >1) {
            _numberOfGoodsForCar = _numberOfGoodsForCar -1;
        }
        
        
    }else{
        
        _numberOfGoodsForCar = _numberOfGoodsForCar +1;
        
        
    }
    [_tableView reloadData];
}

-(void)queDingBtnClick{
    
    if (_yanseID||_chicunID) {
        jiaRuGouWuChe*jR=[[jiaRuGouWuChe alloc]init];
        jR.attr1Id=[NSString stringWithFormat:@"%d",_yanseID];
        jR.attr2Id= [NSString stringWithFormat:@"%d", _chicunID];
        jR.num=[NSString stringWithFormat:@"%d", _numberOfGoodsForCar];
        jR.productId = self.productId;
        jR.shopId =  self.shopID;
        NSLog(@"%d,%d",_yanseID,_chicunID);
        
        if (_numberOfGoodsForCar ==0) {
            SHOW_ALERT(@"请输入购买商品的数量");
        }else{
             
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    //HUD.textLabel.text = @"正在加载中";
    
    HUD.useProgressIndicatorView = YES;
 
    [HUD showInView:self.navigationController.view];

            [HttpsManager requestToGetJiaRuGouWuCheCompletion:jR completion:^(int error, NSString *errmsg)
             {
                 NSString*str=[NSString stringWithFormat:@"%d",error];
                 if ([str isEqualToString:@"0"])
                 {
                     
                     //  SHOW_ALERT(errmsg);
                     [GMDCircleLoader hideFromView:self.view animated:YES];
                     [_tableView  removeFromSuperview];
                     
                     ShoppingOfCartVC *car =[[ShoppingOfCartVC alloc] init];
                     [self.navigationController pushViewController:car animated:YES];
                     
                     NSString *num = [NSString stringWithFormat:@"%d",_numberOfGoodsForCar];
                     self.yanAndChiBlock(@"OK",_yanse,_chima,num,jR.attr1Id,jR.attr2Id);
                     [HUD dismiss];
                      return ;
                 }
                 SHOW_ALERT(@"添加失败，请重新添加");
                
                 
             }];
            
        }
        
        
    }else{
        SHOW_ALERT(@"该商品已售罄！");
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
